package me.leon.ext.java

import java.nio.charset.Charset
import org.junit.Assert
import org.junit.Test

class Base64Test {

    @Test
    fun encode() {
        val raw = "Leon"
        val rawBytes = byteArrayOf(76, 101, 111, 110)
        val expected = "TGVvbg=="

        Assert.assertEquals(expected, Base64Utils.encode(raw))
        Assert.assertEquals(
            expected,
            Base64Utils.encode2ByteArray(rawBytes).toString(Charset.defaultCharset())
        )
    }

    @Test
    fun decode() {
        val raw = "TGVvbg=="
        val expectedBytes = byteArrayOf(76, 101, 111, 110)
        val expected = "Leon"
        Assert.assertArrayEquals(expectedBytes, Base64Utils.decode(raw))
        Assert.assertArrayEquals(expected.toByteArray(), Base64Utils.decode(raw))
        Assert.assertArrayEquals(expectedBytes, Base64Utils.decode(raw.toByteArray()))
        Assert.assertArrayEquals(expected.toByteArray(), Base64Utils.decode(raw.toByteArray()))
    }
}
